package com.apanal.qlife.sys.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.apanal.qlife.common.constants.Constants;
import com.apanal.qlife.common.exception.BusinessException;
import com.apanal.qlife.common.util.FileOperateUtil;
import com.apanal.qlife.sys.model.File;
import com.apanal.qlife.sys.service.FileService;

/**
 * 文件控制类
 * 
 * @author shuliangxing
 * 
 * @date 2015-1-14上午11:22:58
 */
@Controller
@RequestMapping("file")
public class FileController {

	@Autowired
	private FileService fileService;

	/**
	 * 上传文件
	 * 
	 * @param request
	 * @param type
	 *            文件类型
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping("/{type}/uploadFile")
	public List<File> uploadFile(HttpServletRequest request,
			@PathVariable("type") String type) throws IOException {
		// 文件类型
		String filType = "";
		// 文件类型名称
		String filTypeName = "";
		// 文件类型路径
		String typePath = "";
		switch (type) {
		case Constants.CORP_IMG:
			filType = Constants.CORP_IMG;
			filTypeName = Constants.CORP_IMG_NAME;
			typePath = Constants.CORP_IMG_PATH;
			break;
		case Constants.BUSINESS_IMG:
			filType = Constants.BUSINESS_IMG;
			filTypeName = Constants.BUSINESS_IMG_NAME;
			typePath = Constants.BUSINESS_IMG_PATH;
			break;
		case Constants.TAX_REG_CERTIF_IMG:
			filType = Constants.TAX_REG_CERTIF_IMG;
			filTypeName = Constants.TAX_REG_CERTIF_IMG_NAME;
			typePath = Constants.TAX_REG_CERTIF_PATH;
			break;
		case Constants.ORG_IMG:
			filType = Constants.ORG_IMG;
			filTypeName = Constants.ORG_IMG_NAME;
			typePath = Constants.ORG_IMG_PATH;
			break;
		case Constants.AGREEMENT_IMG:
			filType = Constants.AGREEMENT_IMG;
			filTypeName = Constants.AGREEMENT_IMG_NAME;
			typePath = Constants.AGREEMENT_IMG_PATH;
			break;
		case Constants.LOGO_IMG:
			filType = Constants.LOGO_IMG;
			filTypeName = Constants.LOGO_IMG_NAME;
			typePath = Constants.LOGO_IMG_PATH;
			break;
		case Constants.APK:
			filType = Constants.APK;
			filTypeName = Constants.APK_NAME;
			typePath = Constants.APK_PATH;
			break;
		case Constants.FEEDBACK_IMG:
			filType = Constants.FEEDBACK_IMG;
			filTypeName = Constants.FEEDBACK_NAME;
			typePath = Constants.FEEDBACK_PATH;
			break;
		default:
			throw new BusinessException("文件类型不正确");
		}
		return fileService.uploadFile(request, filType, filTypeName, typePath);
	}

	/**
	 * 删除文件
	 * 
	 * @param request
	 * @param path
	 */
	@ResponseBody
	@RequestMapping("deleteFile")
	public void deleteFile(HttpServletRequest request, String path, Long fileId) {
		// 文件上传路径
		String basePath = Constants.FILE_UPLOAD_PATH;
		// 如果服务器上传跟路径为空则使用默认路径
		if (StringUtils.isEmpty(basePath)) {
			basePath = FileOperateUtil.DEFAULT_UPLOADDIR;
		}
		// 如果服务器上传跟路径不是绝对路径,则把文件存储到项目根目录下
		if (!basePath.startsWith("/")) {
			basePath = request.getSession().getServletContext()
					.getRealPath(basePath);
		}

		String delPath = basePath + "/" + path;
		if (fileId != null) {
			fileService.deleteById(fileId);
		}
		FileOperateUtil.deleteFile(delPath);
	}

}